package com.sunny.ynhz.ui.rivermsg;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioGroup;

import com.sunny.ynhz.R;
import com.sunny.ynhz.ui.base.BaseFragment;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/7/14/014.
 */

public class RiverComprehensiveFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener {
    Fragment currFragment;
    @BindView(R.id.rg)
    RadioGroup rg_type;
    @BindView(R.id.fragment_ll)
    View fragment_ll;
    private BaseMsgFragment baseMsgFragment;
    private RiverQualityFragment instanceRiverQualityFragment;
    private RiverRroFragment instanceRiverProFragment;

    /**
     * 当前的河道信息页面
     *
     * @return
     */

    public static RiverComprehensiveFragment getInstanceComprehensiveFragment() {
        return new RiverComprehensiveFragment();
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_compre_river;
    }

    @Override
    protected void setUpView() {
        super.setUpView();
        rg_type.setOnCheckedChangeListener(this);
        baseMsgFragment = BaseMsgFragment.getInstanceBaseMsgFragment();
        instanceRiverQualityFragment = RiverQualityFragment.getInstanceRiverQualityFragment();
        instanceRiverProFragment = RiverRroFragment.getInstanceRiverProFragment();
        swithFragment(baseMsgFragment, null);


    }

    @Override
    protected void init() {
        super.init();
    }

    /**
     * @param nowFragment  当前的fragment
     * @param nextFragment 即将切换的fragment
     */
    public void swithFragment(Fragment nowFragment, Fragment nextFragment) {
        currFragment = nextFragment;
        FragmentManager supportFragmentManager = getChildFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        if (nextFragment == null) {
            fragmentTransaction.add(R.id.fragment_ll, nowFragment).commit();
            currFragment = nowFragment;
        } else {
            if (nextFragment.isAdded()) {
                fragmentTransaction.hide(nowFragment).show(nextFragment).commit();
            } else {
                fragmentTransaction.hide(nowFragment).add(R.id.fragment_ll, nextFragment).commit();

            }
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        if (i == R.id.rb_1) {
            swithFragment(currFragment,baseMsgFragment);
        } else if(i==R.id.rb_2) {
            swithFragment(currFragment,instanceRiverQualityFragment);

        }
        else if(i==R.id.rb_3){
            swithFragment(currFragment,instanceRiverProFragment);

        }

    }
}
